﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ALBC_REST_SDK.ALBCUnit
{
    public class ImageHelper
    {
        public static byte[] GetURLFileStream(string url)
        {
            HttpWebRequest req;
            HttpWebResponse resp;

            req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "GET";
            req.AllowAutoRedirect = true;
            req.Accept = "*/*";
            req.KeepAlive = true;
            req.Proxy = null;

            resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream(); // got image stream       
            if (resp.ContentType.ToLower().StartsWith("image/"))
            {
                int imgLong = (int)resp.ContentLength;
                List<byte> arrayByte = new List<byte>();
                byte[] arrayByteTemp = new byte[1024];
                int l = 0;
                while (l < imgLong)
                {
                    int i = stream.Read(arrayByteTemp, 0, 1024);
                    for(int k = 0;k < i;k++)
                    {
                        arrayByte.Add(arrayByteTemp[k]);
                    }
                    l += i;
                }
                stream.Close();
                resp.Close();
                return arrayByte.ToArray();
            }
            return null;
        }
    }
}
